﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using FrameWork.Entity;
using FrameWork.web.Code;
using FrameWork.Components;

namespace FrameWork.web.Module.ITMS.Service.ServicePostil
{
    public partial class SerDocPostil : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblRepNo.Text = "";
                lblRepName.Text = "";
                lblPostilState.Text = "-1";
                lblSolvePSNNo.Text = "";
                lblSolvePSNName.Text = "";
                //txtDocDatBegin.Text = DateTime.Now.ToString("yyyy-MM") + "-01";
                lblDocDatBegin.Text = DateTime.Now.ToString("yyyy-MM") + "-01";//txtDocDatBegin.Text.Trim();
                //txtDocDatEnd.Text = DateTime.Now.ToString("yyyy-MM-dd");
                lblDocDatEnd.Text = DateTime.Now.ToString("yyyy-MM-dd");
                BindServerDoc();
            }
        }


        private void BindServerDoc()
        {
            try
            {
                string repUserNo = lblRepNo.Text.Trim();
                string repUserName = lblRepName.Text.Trim();
                string docSolvPsnNo = lblSolvePSNNo.Text.Trim();
                string docSolvPsnName = lblSolvePSNName.Text.Trim();
                string docBeginTime = lblDocDatBegin.Text.Trim() + " 00:01";
                string docEndTime = lblDocDatEnd.Text.Trim() + " 23:59";
                int state = int.Parse(lblPostilState.Text.Trim());
                its_ServiceDocCs its = new its_ServiceDocCs();
                GvDoc.DataSource = its.ServiceDoc_PostilSelect(repUserNo, repUserName, docSolvPsnNo, docSolvPsnName, docBeginTime, docEndTime, state, PagerDoc.CurrentPageIndex, PagerDoc.PageSize);
                GvDoc.DataBind();
                PagerDoc.RecordCount = its.ServiceDoc_PostilSelectCount(repUserNo, repUserName, docSolvPsnNo, docSolvPsnName, docBeginTime, docEndTime, state);
            }
            catch (Exception ex)
            {
                FileTxtLogs.WriteLog("查询出错,错误原因:" + ex.Message);
                string msg = "查询出错,错误原因:" + ex.Message.Replace("\n", "\\n");
                ClientScript.RegisterStartupScript(this.GetType(), "Ware3", "<script>alert('" + msg + "');</script>");
            }
        }

        protected void BtnQuery_Click(object sender, EventArgs e)
        {
            lblRepNo.Text = (string)Common.sink(txtRepNo.UniqueID, MethodType.Post, 20, 0, DataType.Str);
            lblRepName.Text = (string)Common.sink(txtRepName.UniqueID, MethodType.Post, 60, 0, DataType.Str);
            lblSolvePSNNo.Text = (string)Common.sink(txtSolvePSNNo.UniqueID, MethodType.Post, 20, 0, DataType.Str);
            lblSolvePSNName.Text = (string)Common.sink(txtSolvePSNName.UniqueID, MethodType.Post, 60, 0, DataType.Str);
            DateTime dtBegin = (DateTime)Common.sink(txtDocDatBegin.UniqueID, MethodType.Post, 20, 1, DataType.Dat);
            lblDocDatBegin.Text = dtBegin.ToString("yyyy-MM-dd");
            /*
            if (lblDocDatBegin.Text.Trim() == "")
            {
                string msg = "开始时间不能为空！";
                ClientScript.RegisterStartupScript(this.GetType(), "Ware1", "<script>alert('" + msg + "');</script>");
                return;
            }
             */
            DateTime dtEnd = (DateTime)Common.sink(txtDocDatEnd.UniqueID, MethodType.Post, 20, 1, DataType.Dat);
            lblDocDatEnd.Text = dtEnd.ToString("yyyy-MM-dd");
            /*
            if (lblDocDatEnd.Text.Trim() == "")
            {
                string msg = "结束时间不能为空！";
                ClientScript.RegisterStartupScript(this.GetType(), "Ware2", "<script>alert('" + msg + "');</script>");
                return;
            }
             */

            lblPostilState.Text = "-1";
            if ((chbPostil.Checked) && (!chbunPostil.Checked))
            {
                lblPostilState.Text = "1";
            }
            else if ((!chbPostil.Checked) && (chbunPostil.Checked))
            {
                lblPostilState.Text = "0";
            }
            //txtDocDatBegin.Text = lblDocDatBegin.Text.Trim();
            //txtDocDatEnd.Text = lblDocDatEnd.Text.Trim();
            PagerDoc.CurrentPageIndex = 1;
            BindServerDoc();
        }

        protected void PagerSolve_PageChanged(object sender, EventArgs e)
        {
            BindServerDoc();
        }

        protected void lbtUnsolve_Click(object sender, EventArgs e)
        {
            try
            {

                //if (((LinkButton)sender).Parent.Parent.Parent.Parent.ID == "GVUnsolve")
                //{
                //    txtUid.Text = ((Label)((GridViewRow)((LinkButton)sender).Parent.Parent).FindControl("labUID")).Text.Trim();
                //    showTR();
                //    GetDocDetail(txtUid.Text.Trim());
                //}
                //else
                //{
                //    hindTR();
                //}
                string strServDocNo = ((LinkButton)sender).Text;
                txtDocNo.Text = strServDocNo;
                its_ServiceDocCs sdCs = new its_ServiceDocCs();
                its_ServiceDoc sd = new its_ServiceDoc();
                sd.ServDocNo = strServDocNo;
                DataTable dt = sdCs.ServiceSelect(sd);
                //txtServDocNo.Text = strServDocNo;
                txtRepContext.Text = dt.Rows[0]["RepContent"].ToString();
                txtRepTitle.Text = dt.Rows[0]["RepTitle"].ToString();
                txtSolution.Text = dt.Rows[0]["ItemMessage"].ToString().Replace("\\n", "\n");
                txtSolution.Text = txtSolution.Text.Replace("\\t", "\t");

                its_ServiceDocExtEntity ut = BusinessFacade.its_ServiceDocExtDisp(strServDocNo);
                string postilStr = ut.PostilDesc.Trim();
                txtPostil.Text = postilStr.Replace("\\n", "\n");
                txtPostil.Text = postilStr.Replace("\\t", "\t");

                if (ut.PostilPSN.Trim() != "")
                {
                    lbl_PostilPSN.Text = "批注人员:" + ut.PostilPSN;
                    lbl_PostilTime.Text = "批注时间:" + ut.PostilTime;
                }
                else
                {
                    lbl_PostilPSN.Text = "";
                    lbl_PostilTime.Text = "";
                }
            }
            catch
            {
                //
            }
        }

        protected void btnPostil_Click(object sender, EventArgs e)
        {
            if (txtDocNo.Text.Trim() == "")
            {
                string msg = "没有服务单信息，不能批注！";
                ClientScript.RegisterStartupScript(this.GetType(), "Ware21", "<script>alert('" + msg + "');</script>");
                return;
            }

            if (txtPostil.Text.Trim() == "")
            {
                string msg = "没有批注信息！";
                ClientScript.RegisterStartupScript(this.GetType(), "Ware22", "<script>alert('" + msg + "');</script>");
                return;
            }

            sys_UserTable sysut = BusinessFacade.sys_UserDisp(Common.Get_UserID);
            string User_No = sysut.U_UserNO;
            string User_Name = sysut.U_CName.Trim();
            if (User_Name.Trim() == "")
            {
                EventMessage.MessageBox(1, "操作失败", "操作失败,无法获取当前归纳人员信息!", Icon_Type.Alert, Common.GetHomeBaseUrl("SerDocPostil.aspx"));
                return;
            }

            
            its_ServiceDocExtEntity utexit = BusinessFacade.its_ServiceDocExtDisp(txtDocNo.Text.Trim());

            string DocNo_Value = (string)Common.sink(txtDocNo.UniqueID, MethodType.Post, 20, 1, DataType.Str);
            string PostilPSNNO_Value = User_No.Trim();
            string PostilPSN_Value = User_Name.Trim();
            string PostilDesc_Value = (string)Common.sink(txtPostil.UniqueID, MethodType.Post, 0, 0, DataType.Str);
            int PostilState_Value = 1;
            DateTime PostilTime_Value = DateTime.Now;


            if (utexit.DocNo.Trim() != "")
            {
                int UID = utexit.UID;
                its_ServiceDocExtEntity utUpdate = BusinessFacade.its_ServiceDocExtDisp(UID);
                utUpdate.DocNo = DocNo_Value;
                utUpdate.PostilPSNNO = PostilPSNNO_Value;
                utUpdate.PostilPSN = PostilPSN_Value;
                utUpdate.PostilDesc = PostilDesc_Value;
                utUpdate.PostilTime = PostilTime_Value;
                utUpdate.PostilState = PostilState_Value;
                utUpdate.DataTable_Action_ = DataTable_Action.Update;

                Int32 rInt = BusinessFacade.its_ServiceDocExtInsertUpdateDelete(utUpdate);

                if (rInt > 0)
                {
                    string OpTxt = string.Format("修改成功!(ID:{0})", UID);
                    EventMessage.MessageBox(1, "操作成功", OpTxt, Icon_Type.OK, Common.GetHomeBaseUrl("SerDocPostil.aspx"));
                }
                else if (rInt == -2)
                {
                    EventMessage.MessageBox(1, "操作失败", "操作失败,存在相同的键值(用户名/数据)!", Icon_Type.Alert, Common.GetHomeBaseUrl("SerDocPostil.aspx"));
                }
                else
                {
                    EventMessage.MessageBox(1, "操作失败", string.Format("操作失败,返回值:{0}!", rInt), Icon_Type.Error, Common.GetHomeBaseUrl("SerDocPostil.aspx"));
                }
            }
            else
            {
                its_ServiceDocExtEntity ut = BusinessFacade.its_ServiceDocExtDisp(-1);
                ut.DocNo = DocNo_Value;
                ut.PostilPSNNO = PostilPSNNO_Value;
                ut.PostilPSN = PostilPSN_Value;
                ut.PostilDesc = PostilDesc_Value;
                ut.PostilTime = PostilTime_Value;
                ut.PostilState = PostilState_Value;

                ut.DataTable_Action_ = DataTable_Action.Insert;
                Int32 rInt = BusinessFacade.its_ServiceDocExtInsertUpdateDelete(ut);

                if (rInt > 0)
                {
                    string OpTxt = string.Format("增加成功!(ID:{0})", rInt);
                    EventMessage.MessageBox(1, "操作成功", OpTxt, Icon_Type.OK, Common.GetHomeBaseUrl("SerDocPostil.aspx"));
                }
                else if (rInt == -2)
                {
                    EventMessage.MessageBox(1, "操作失败", "操作失败,存在相同的键值(用户名/数据)!", Icon_Type.Alert, Common.GetHomeBaseUrl("SerDocPostil.aspx"));
                }
                else
                {
                    EventMessage.MessageBox(1, "操作失败", string.Format("操作失败,返回值:{0}!", rInt), Icon_Type.Error, Common.GetHomeBaseUrl("SerDocPostil.aspx"));
                }
            }
        }

        protected void GvDoc_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (((Label)e.Row.FindControl("labPostilState")).Text == "1")
                    {
                        e.Row.BackColor = System.Drawing.Color.Red;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
